package fr.toto.triseries;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentSkipListSet;

import com.kokotchy.betaSeriesAPI.model.Episode;
import com.kokotchy.betaSeriesAPI.model.Show;

import fr.toto.comparators.ShowComparator;

public class SeriesSet {

	private static ConcurrentSkipListSet<Show> s = new ConcurrentSkipListSet<Show>(
			new ShowComparator());

	public static synchronized void add(Show sh) {
		s.add(sh);
	}

	public static synchronized void clear() {
		s.clear();
	}

	public static synchronized Show findShow(String url) {
		Show sh = new Show();
		sh.setUrl(url);
		return s.tailSet(sh, true).floor(sh);
	}
	
	public static synchronized String searchUrl(String title) {
		Show sh = Regex.getBestUrl(s,title, 10);
		if (sh == null) return "";
		return sh.getUrl();
	}

	public static synchronized Show findShow(Show sh) {
		return s.tailSet(sh, true).floor(sh);
	}

	public static synchronized Show findShow(Episode ep) {
		Show sh = new Show();
		sh.setUrl(ep.getShowUrl());
		return findShow(sh);
	}

	public static synchronized boolean isEmpty() {
		return s.isEmpty();
	}

	public static synchronized void addAll(List<Show> shows) {
		for (Show sh : shows) {
			add(sh);
		}
	}

	public static synchronized boolean isNew(String url) {
		return (findShow(url) == null);
	}
	
	public static synchronized boolean isArchived(String url) {
		Show sh = findShow(url);
		if(sh == null) return false;
		return sh.isArchived();
	}

	public static synchronized Set<Show> ArchivedShows() {
		Set<Show> shs = new TreeSet<Show>(new ShowComparator());
		for(Show sh : s) {
			if(sh.isArchived()) shs.add(sh);
		}
		return shs;
	}

}
